package example.controller;

import com.github.pagehelper.PageInfo;
import example.entity.Goods;
import example.entity.Goodstype;
import example.service.GoodstypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

@Controller
public class TypesCtrl {

    @Resource
    GoodstypeService goodstypeService;

    @RequestMapping(value = "/types")
    public String getGoodsTypesList(Model model,
                               @RequestParam(value = "pageNum",required = false,defaultValue ="1" )Integer pageNum,
                               @RequestParam(value = "pageSize",required = false,defaultValue = "3")Integer pageSize) {
        ArrayList<Goodstype> goodTypeList = goodstypeService.queryAll();

        model.addAttribute("goodstypesList", goodTypeList);
        return "types";
    }

    @ApiOperation("根据ID删除类别")
    @GetMapping("delGoodsType/{id}")
    public String delGoods(@PathVariable("id") int id , Model model) {
        int num = goodstypeService.deleteById(id);
        model.addAttribute("msg","删除成功！");
        return  "redirect:/types";

    }

    @PostMapping("/editGoodsType")
    public String editGoods(Goodstype goodstype,Model model){
        System.out.println(goodstype);
        int num = goodstypeService.update(goodstype);
        model.addAttribute("msg","商品信息过更新成功！");
        //        ArrayList<Goodstype> goodstypesList = goodstypeService.queryAll();
//        model.addAttribute("goodstypesList",goodstypesList);
        return "forward:/types";
    }

    @RequestMapping("/saveType")
    public String save(Goodstype goodstype){
        System.out.println(goodstype.getTname());
        int goodstypeTemp = goodstypeService.save(goodstype);
        if (goodstypeTemp != 0) {
            return "redirect:/types";
        }
        return "500";
    }

    @GetMapping("/gototypesAdd")
    public String gotoGoodsAdd(){

        return "typesAdd";

}
}
